package com.ziyun.pop.web.modules.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.utility.StringUtils;
import com.ziyun.pop.common.e.DeleteEnum;
import com.ziyun.pop.common.e.logistics.LogisticsLogiTypeEnum;
import com.ziyun.pop.common.e.logistics.LogisticsSendTypeEnum;
import com.ziyun.pop.common.e.order.*;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.erpOrderLog.entity.ErpOrderLogEntity;
import com.ziyun.pop.modules.order.dto.ErpOrderItemDto;
import com.ziyun.pop.modules.order.entity.*;
import com.ziyun.pop.modules.order.dto.OrderSupplierDto;
import com.ziyun.pop.modules.order.service.ErpOrderService;
import com.ziyun.pop.modules.production.entity.ErpOrderItemEntity;
import com.ziyun.pop.modules.production.vo.ErpOrderProductionListVo;
import com.ziyun.pop.modules.supp.entity.SellerInfoEntity;
import com.ziyun.pop.modules.userWallet.entity.UserWalletEntity;
import io.renren.common.utils.PageUtils;
import com.ziyun.pop.modules.production.entity.ErpOrderItemEntity;
import com.ziyun.pop.modules.production.service.ErpOrderItemService;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import net.sf.json.JSONArray;
import org.apache.catalina.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import java.math.BigDecimal;
import java.util.*;
import java.util.Map;

/**
 * 图文宝订单
 *
 * @author llc
 * @date 2018-12-21 11:19:18
 */
@RequestMapping("twbOrder/erporder")
@RestController
public class TwbOrderController {
    @Autowired
    private ErpOrderService erpOrderService;

    @Autowired
    private ErpOrderItemService erpOrderItemService;

    @RequestMapping("/list")
    /*@RequiresPermissions("order:erporder:list")*/
    public R list(@RequestBody Map<String, Object> params){
        Long sellerId=UserUtils.getCurentUser().getSellerId();
        params.put("sellerId",sellerId);
        return erpOrderService.queryPageTwbOrderList(params);
    }

    @RequestMapping("/querySearchProduct")
    /*@RequiresPermissions("order:erporder:list")*/
    public R querySearchProduct(@RequestBody Map<String, Object> params){
        Long sellId=UserUtils.getCurentUser().getSellerId();
        params.put("sellerId",UserUtils.getCurentUser().getSellerId());
        return erpOrderService.querySearchProduct(params);
    }

    /**
     * 添加订单查询相关数据
     * @return
     */
    @RequestMapping("/addData")
    /*@RequiresPermissions("order:erporder:list")*/
    public R addData(){
        Long sellerId=UserUtils.getCurentUser().getSellerId();
        String username=UserUtils.getCurentUser().getUsername();
        return erpOrderService.addData(sellerId,username);
    }

    @RequestMapping("/searchMember")
    public R searchMember(@RequestBody Map<String, Object> params ){
        Long sellerId=UserUtils.getCurentUser().getSellerId();
        params.put("sellerId",sellerId);
        return erpOrderService.searchMember(params);
    }

    /**
     *添加订单
     */
    @RequestMapping("/save")
    @ResponseBody
    /* @RequiresPermissions("order:erporder:save")*/
    public R save(@RequestBody ErpOrderItemDto erpOrderItemDto){
        erpOrderItemDto.getErpOrder().setThirdMerchantId(UserUtils.getCurentUser().getSellerId().intValue());
        return    erpOrderService.saveTwbOrder(erpOrderItemDto);
    }



    /**
     * 修改订单
     */
    @RequestMapping("/update")
    // @RequiresPermissions("order:erporder:info")
    public R update(@RequestBody ErpOrderItemDto erpOrderItemDto){
        return  erpOrderService.updateTwbOrder(erpOrderItemDto);
    }

    /**
     * 订单信息用于修改订单
     */
    @RequestMapping("/orderInfo/{orderNo}")
    // @RequiresPermissions("order:erporder:info")
    public R orderInfo(@PathVariable("orderNo") String orderNo){
        return  erpOrderService.twbUpdateOrderInfo(orderNo);
    }

    /**
     * 查看详情信息
     */
    @RequestMapping("/info/{orderNo}")
    // @RequiresPermissions("order:erporder:info")
    public R info(@PathVariable("orderNo") String orderNo){
        return  erpOrderService.twbOrderInfo(orderNo);
    }


    /**
     * 图文宝添加供应商
     * @param orderSupplierDto
     * @return
     */
    @RequestMapping("/twbsuppSave")
    public R twbSuppSave(@RequestBody OrderSupplierDto orderSupplierDto){
            Long userId = UserUtils.getCurentUser().getUserId();
            return erpOrderService.twbSuppSave(orderSupplierDto, userId);
    }

    /**
     * 图文宝删除分配信息
     * @param productionId
     * @return
     */
    @RequestMapping("/twbdeleteAllotItem")
    public R twbdeleteAllotItem(@RequestParam("productionId") Integer productionId){
        return erpOrderService.twbdeleteAllotItem(productionId);
    }

    /**
     * 批量审核(只限于商城订单)
     * @param
     * @return
     */
    @RequestMapping("/batchAudit")
    public R batchAudit(@RequestParam("orderNos") String orderNos ){
        String auditName=UserUtils.getCurentUser().getUsername();
        String createBy=UserUtils.getCurentUser().getUserId().toString();
        return   erpOrderService.batchAudit(auditName,orderNos,createBy);
    }

    /**
     * 生产完成(如果商城订单并且是自提，则直接将状态改为已完成)
     * @param
     * @return
     */
    @RequestMapping("/finishOrderItem")
    public R finishOrderItem(@RequestParam("orderNos") String orderNos ){
        String createName=UserUtils.getCurentUser().getUsername();
        String createBy=UserUtils.getCurentUser().getUserId().toString();
        return  erpOrderService.finishTwbOrder(createName,orderNos,createBy);
    }

    /***
     * 取消图文宝订单
     * @param erpOrderLogs
     * @return
     */
    @RequestMapping("/cancelOrder")
    public R cancelOrder (@RequestBody ErpOrderLogEntity erpOrderLogs){
        erpOrderLogs.setCreateName(UserUtils.getCurentUser().getUsername());
        erpOrderLogs.setCreateBy(UserUtils.getCurentUser().getSellerId().toString());
        //取消订单
        return erpOrderService.cancelTwbOrder(erpOrderLogs);
    }

    //收银前查询的相关数据
    @RequestMapping("/getReceviceMoneyData/{orderNo}")
    public R getReceviceMoneyData(@PathVariable String orderNo ){
        return  erpOrderService.getTwbReceviceMoneyData(orderNo);
    }

    //收银结算
    @RequestMapping("/saveReceviceMoney")
    public R saveReceviceMoneyData(@RequestBody Map<String,Object> params ) {
        return erpOrderService.saveTwbReceviceMoneyData(params);
    }

    //查询twb添加物流所需参数
    @RequestMapping("/twbLogisticInfo/{orderNo}")
    public R twbLogisticInfo(@PathVariable String orderNo){
        //查询订单信息
        if(!StringUtils.isNotBlank(orderNo)){
            return  R.error("订单号获取异常，请联系系统管理员!");
        }
        Long sellId=UserUtils.getCurentUser().getSellerId();
        return  erpOrderService.twbLogisticInfo(orderNo,sellId);
    }

    /**
     * 图文宝月结订单查询
     * @param params
     * @return
     */
    @RequestMapping("/monthList")
    /*@RequiresPermissions("order:erporder:list")*/
    public R monthList(@RequestBody Map<String, Object> params){
        Long sellerId=UserUtils.getCurentUser().getSellerId();
        params.put("sellerId",sellerId);
        params.put("onCredit",1);
        return erpOrderService.queryMonthTwbOrderList(params);
    }

    /**
     * 图文宝月结批量收银前查询参数
     * @param
     * @return
     */
    @RequestMapping("/getMonthMoneyData")
    public R getTwbMonthMoneyData(@RequestParam("orderNos") String orderNos ){
        return   erpOrderService.getTwbMonthMoneyData(orderNos);
    }

    @RequestMapping("/saveMonthMoney")
    public R saveTwbMonthMoney(@RequestBody Map<String,Object> params ){
        return   erpOrderService.saveTwbMonthMoney(params);
    }
}
